var app = getApp();
Page({
    data:{
        curPage : '/zofui_sales/pages/shop/list',
        page : {
            isend : false,
            doing : false,
            doo : 'shop_v1',
            pdata : {
                op : 'list',
                page : 1,
                type : 0,
                isinit : 1,
                sortid : 0,
                sorttwo : 0,
                search : '',
                cityid : 0,
                isyz : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        params:{shopbg:'#ffffff'},
        datalist : [],
        isIPX : app.globalData.isIPX,
        parents : 0,
        down : [],
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        Time : app.globalData.Time,
        option0 : [
            { text: '全部类型', value: 0 },
            { text: '优质', value: 1 },
        ],
        option2 : [
            { text: '全部分类', value: 0 },
        ],
        option3 : [
            { text: '距离最近', value: 0 },
            { text: '好评优先', value: 1 },
            { text: '销量优先', value: 2 },
        ],
        color : '',
        isdrop : false,
        scrollTop : 0,
        citylist : [],
        showdown : false,
        tempsort : [],
        shopsort : [],
    },
    onShow: function () {
        var self = this;
        wx.hideTabBar();
        var loc = wx.getStorageSync('nlloc');
        if( loc.latitude ){
            self.setData({
                'page.pdata.lng' : loc.longitude,
                'page.pdata.lat' : loc.latitude,
            });
        }
    },
    onLoad:function(options){
        var self = this;

        self.setData({
            'page.pdata.status' : options.status ? options.status : 0,
            options : options,
            'page.pdata.zfid' : options.zfid ? options.zfid : '',
        });
		var shopid = options.shopid ? options.shopid : 0;
        
        app.com.auth(function(user){
            var loc = wx.getStorageSync('nlloc');
            
            if( loc.latitude ){
                self.setData({
                    'page.pdata.lng' : loc.longitude,
                    'page.pdata.lat' : loc.latitude,
                });
            }
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                var color = app.com.mainColor(data.params.style);

                self.setData({
                    inited : true,
                    datalist : data.list,
                    color : color,
                    option0 : data.option0,
                })
                
                setTimeout(function(){
                    self.setData({
                       Pagetheme : data.params.style,
                       params : data.params,
                       sharedata : res.data.data.sharedata,
                       sort : res.data.data.sort,
                       citylist : res.data.data.citylist,
                       userinfo : res.data.data.userinfo ? res.data.data.userinfo : {},
                       'page.pdata.isinit' : 0,
                       'page.pdata.type' : data.params.shopdesc,
                       cardinfo : data.cardinfo ? data.cardinfo : {},
                       shopsort : data.shopsort ? data.shopsort : [],
                    });
                    self.data.tempsort = res.data.data.sort;

                    wx.setNavigationBarTitle({
                      title:  res.data.data.params.title ? res.data.data.params.title : '好店',
                    });

                    if( data.params.shopstl == 0 ){
                        var color = res.data.data.params.shopbg ? res.data.data.params.shopbg : '#000000';
                        wx.setNavigationBarColor({
                            frontColor: '#ffffff',
                            backgroundColor: color,
                        });
                    }else{
                        wx.setNavigationBarColor({
                            frontColor: '#000000',
                            backgroundColor: '#ffffff',
                        });
                    }

                    app.com.getLocation(self,function(loc){
                        self.setData({
                            'page.pdata.lng' : loc.longitude,
                            'page.pdata.lat' : loc.latitude,
                        });
                    });
                   
                },5)

            },false,function(res){
                app.com.alert(res.data.message);
            });
			app.com.setBar(app,self,shopid);
        });

    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            'page.pdata.isinit' : 1,
        });
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    changeorder(e){
        var a = {
            currentTarget : {
                dataset : {
                    type : e.detail
                }
            }
        }
        this.change(a);
    },
    change(e){
        var self = this;
        if( self.data.ising || e.currentTarget.dataset.type == self.data.page.pdata.type ) return false;
        self.data.ising = true;
        self.setData({
            'page.pdata.type' : e.currentTarget.dataset.type,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    changecity(e){
        var self = this;
        if( self.data.ising || e.detail == self.data.page.pdata.cityid ) return false;
        self.data.ising = true;
        self.setData({
            'page.pdata.cityid' : e.detail,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    changeyz(e){
        var self = this;
        if( self.data.ising || e.detail == self.data.page.pdata.isyz ) return false;
        self.data.ising = true;
        self.setData({
            'page.pdata.isyz' : e.detail,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    showJoin(e){
        var self = this;
        self.setData({
            Joinanimation : !self.data.Joinanimation,
        })
    },
    stype(e){
        this.setData({
            sstype : e.currentTarget.dataset.type
        });
    },
    sorder(e){
        var self = this;
        if( self.data.ising || e.currentTarget.dataset.o == self.data.page.pdata.type ) return false;
        self.data.ising = true;

        self.setData({
            datalist : [],
            'page.pdata.type' : e.currentTarget.dataset.o,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            sstype : 0,
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
            wx.pageScrollTo({
                scrollTop: 0
            });
        });
        self.data.ising = false;

    },
    changesortone(e){
        var a = {
            currentTarget : {
                dataset : {
                    sid : e.detail,
                    stwo : 0
                }
            }
        }
        this.ssort(a);

        var sort = [];
        for (var i = 0; i < this.data.tempsort.length; i++) {
            let item =  this.data.tempsort[i];
            if(item.parent*1 == e.detail*1 || e.detail*1 == 0 || item.value == 0){
                sort.push(item);
            }
        }
        this.setData({
            sort : sort
        })
    },
    changesort(e){
        var a = {
            currentTarget : {
                dataset : {
                    sid : this.data.page.pdata.sortid,
                    stwo : e.detail
                }
            }
        }
        this.ssort(a);
    },
    changesortparent(e){
        var index = e.currentTarget.dataset.index*1;
        var sid = this.data.shopsort[index].value;
        var a = {
            currentTarget : {
                dataset : {
                    sid : this.data.shopsort[index].value,
                    stwo : 0,
                }
            }
        }
        this.setData({
            sort : sid == 0 ? this.data.tempsort : this.data.shopsort[index].down,
        })
        this.ssort(a);
    },
    ssort(e){
        var self = this;

        if( self.data.ising ) return false;
        self.data.ising = true;

        var down = [];
        if( e.currentTarget.dataset.type == 1 ){
            for (var i = 0; i < self.data.sort.length; i++) {
                if( self.data.sort[i].id == e.currentTarget.dataset.sid ){
                    down = self.data.sort[i].down ? self.data.sort[i].down : [];
                }
            }
        }
        self.setData({
            datalist : [],
            'page.pdata.sortid' : e.currentTarget.dataset.sid,
            'page.pdata.sorttwo' : e.currentTarget.dataset.stwo ? e.currentTarget.dataset.stwo : 0,
            'page.pdata.search' : '',
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            sstype : 0,
            showdown : false,
            down : down,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    hidedown(){
        this.setData({
            showdown : false,
        })
    },
    showsort(e){
        this.setData({
            parents : e.currentTarget.dataset.sid,
            showdown : true,
        })
    },
    searchshopfn(e){
        var self = this;
        if( self.data.ising ) return false;
        self.data.ising = true;

        self.setData({
            datalist : [],
            'page.pdata.sortid' : 0,
            'page.pdata.sorttwo' : 0,
            'page.pdata.page' : 1,
            'page.pdata.search' : e.detail,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            sstype : 0,
            showdown : false,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    clears(){
        var e = {
            detail : '',
        };
        this.searchshopfn(e);
    },
    hideselect(){
        this.setData({
            sstype : 0
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },

})